<template>
  <el-dialog v-drag :title="!dataForm.receiveId ? '新增收货单' : '修改收货单'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="采购订单号" prop="purchaseOrderNo">
        <el-input size="mini" v-model="dataForm.purchaseOrderNo" placeholder="采购订单号" disabled clearable></el-input>
      </el-form-item>
      <el-form-item label="收货单号" prop="receiveNo">
        <el-input size="mini" v-model="dataForm.receiveNo" placeholder="收货单号" disabled clearable></el-input>
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-select size="mini" @focus="getSuppliers()" @click="getSuppliers()" @change="setSuppliers()" v-model="dataForm.supplierName"
          clearable placeholder="请选择">
          <el-option v-for="item in suppliers" :key="item.supplierId" :label="item.supplierName"
            :value="item.supplierName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收货日期" prop="receiveDate">
        <el-date-picker size="mini" style="width:100%;" v-model="dataForm.receiveDate" value-format="yyyy-MM-dd"
          type="date" clearable placeholder="收货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总费用" prop="totalFee">
        <el-input size="mini" v-model="dataForm.totalFee" placeholder="总费用" disabled clearable></el-input>
      </el-form-item>
      <el-form-item label="温度控制" prop="temperatureControl">
        <el-select size="mini" @focus="getDicts('TEMPERATURE_CONTROL')" @click="getDicts('TEMPERATURE_CONTROL')"  v-model="dataForm.temperatureControl" clearable
          placeholder="请选择">
          <el-option v-for="item in temperatureControls" :key="item.itemId" :label="item.label"  :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运输工具" prop="transportTool">
        <el-select size="mini" @focus="getDicts('TRANSPORT_TOOL')" @click="getDicts('TRANSPORT_TOOL')"  v-model="dataForm.transportTool" clearable
          placeholder="请选择">
          <el-option v-for="item in transportTools" :key="item.itemId" :label="item.label"  :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="到货温度" prop="deliveryTemperature">
        <el-select size="mini" @focus="getDicts('DELIVERY_TEMPERATURE')" @click="getDicts('DELIVERY_TEMPERATURE')"  v-model="dataForm.deliveryTemperature" clearable
          placeholder="请选择">
          <el-option v-for="item in deliveryTemperatures" :key="item.itemId" :label="item.label"  :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运输方法" prop="transportMethod">
        <el-select size="mini" @focus="getDicts('TRANSPORT_METHOD')" @click="getDicts('TRANSPORT_METHOD')"  v-model="dataForm.transportMethod" clearable
          placeholder="请选择">
          <el-option v-for="item in transportMethods" :key="item.itemId" :label="item.label"  :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运输日期" prop="transportDate">
        <el-date-picker size="mini" style="width:100%;" v-model="dataForm.transportDate" value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime" clearable placeholder="运输日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核人名称" prop="auditUser">
        <el-select size="mini" @focus="getUsers()" @change="setUsers()" v-model="dataForm.auditUser" clearable
          placeholder="请选择">
          <el-option v-for="item in users" :key="item.userId" :label="item.username" :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="总部日期" prop="headQuarterDate">
        <el-date-picker size="mini" style="width:100%;" v-model="dataForm.headQuarterDate" value-format="yyyy-MM-dd"
          type="date" clearable placeholder="总部日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总部单号" prop="headQuarterNo">
        <el-input size="mini" v-model="dataForm.headQuarterNo" placeholder="总部单号" clearable></el-input>
      </el-form-item>
      <el-form-item label="收货人" prop="receiveUser">
        <el-select size="mini" @focus="getUsers()" @change="setUsers()" v-model="dataForm.receiveUser" clearable
          placeholder="请选择">
          <el-option v-for="item in users" :key="item.userId" :label="item.username" :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input size="mini" v-model="dataForm.remark" placeholder="备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          receiveId: 0,
          purchaseOrderId: '',
          purchaseOrderNo: '',
          receiveNo: '',
          supplierId: '',
          supplierName: '',
          receiveDate: '',
          totalFee: '',
          temperatureControl: '',
          transportTool: '',
          deliveryTemperature: '',
          transportMethod: '',
          transportDate: '',
          auditUserId: '',
          auditUser: '',
          auditStatus: '',
          auditNote: '',
          headQuarterDate: '',
          headQuarterNo: '',
          receiveUserId: '',
          receiveUser: '',
        },
        users: [],
        suppliers: [],
        temperatureControls: [],
        transportTools: [],
        deliveryTemperatures: [],
        transportMethods: [],
        dataRule: {
          supplierName: [{
            required: true,
            message: '供应商名称不能为空',
            trigger: 'blur'
          }],
          receiveDate: [{
            required: true,
            message: '收货日期不能为空',
            trigger: 'blur'
          }],
          temperatureControl: [{
            required: true,
            message: '温度控制不能为空',
            trigger: 'blur'
          }],
          transportTool: [{
            required: true,
            message: '运输工具不能为空',
            trigger: 'blur'
          }],
          deliveryTemperature: [{
            required: true,
            message: '运输温度不能为空',
            trigger: 'blur'
          }],
          transportMethod: [{
            required: true,
            message: '运输方法不能为空',
            trigger: 'blur'
          }],
          transportDate: [{
            required: true,
            message: '运输日期不能为空',
            trigger: 'blur'
          }],
          auditUser: [{
            required: true,
            message: '审核人名称不能为空',
            trigger: 'blur'
          }],
          receiveUser: [{
            required: true,
            message: '收货人不能为空',
            trigger: 'blur'
          }],
        }
      }
    },
    mounted() {
        // 初始化查询，默认为前一天
        this.dataForm.receiveDate = this.timeDefault;
    },
    methods: {
      init(id) {
        this.dataForm.receiveId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.receiveId) {
            const res = await this.$http({
              url: `/purchase/purchasereceiveorder/info/${this.dataForm.receiveId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
            let data = res.data
            if (data && data.code === 0) {
              this.dataForm = data.purchaseReceiveOrder
            }
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url: `/purchase/purchasereceiveorder/${!this.dataForm.receiveId ? 'save' : 'update'}`,
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          }
        })
      },
      async getSuppliers() {
        const res = await this.$http({
          url: `/product/supplier/getSupplies`,
          method: 'get',
          params: this.$http.adornParams()
        })
        let data = res.data
        if (data && data.code === 0) {
          this.suppliers = data.data
        }
      },
      setSuppliers() {
        for (let index in this.suppliers) {
          let item = this.suppliers[index]
          if (this.dataForm.supplierName === item.supplierName) {
            this.dataForm.supplierId = item.supplierId
            this.dataForm.contactTel = item.mobile
            this.dataForm.contactor = item.salesman
            return
          }
        }
      },
      async getUsers() {
        const res = await this.$http({
          url: `/sys/user/listUsers`,
          method: 'get',
          params: this.$http.adornParams()
        })
        let data = res.data
        if (data && data.code === 0) {
          this.users = data.data
        }
      },
      setUsers() {
        this.users.forEach((item) => {
          if (this.dataForm.auditUser == item.username) {
            this.dataForm.auditUserId = item.userId;
          } else if (this.dataForm.receiveUser == item.username) {
            this.dataForm.receiveUserId = item.userId;
          }
        });
      },
      async getDicts(type) {
        const res = await this.$http({
          url: '/base/basedict/getDict',
          method: 'get',
          params: this.$http.adornParams({
            'code': type
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          if(type === 'TEMPERATURE_CONTROL'){
            this.temperatureControls = data.data
          }else if(type === 'TRANSPORT_TOOL'){
            this.transportTools = data.data
          }else if(type === 'DELIVERY_TEMPERATURE'){
            this.deliveryTemperatures = data.data
          }else if(type === 'TRANSPORT_METHOD'){
            this.transportMethods = data.data
          }
        }
      },
    }
  }
</script>
